<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/controller/ctlAbstract.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busUser.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busOccupation.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busAddress.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busLocation.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/user.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/address.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/generalFunctions.php';

/**
 * This is the class handle the registration of new users
 */
class UpdateController extends AbstractController {
	private $_userBus;
	private $_addressBus;
	private $_occupationBus;
	/**
	 *
	 * This is the default function that will be called by the router
	 * @param array $getVars GET variables posted to index.php
	 */
	public function main(array $getVars) {
		global $ubi;
		
		$this->_userBus = new UserBus();
		$this->_addressBus = new AddressBus();
		$this->_occupationBus = new OccupationBus();
		
		$userID = $_SESSION['userid'];
		
		if ($getVars['type'] == 'user') 	{
			if ($userID) {
				//error list when adding user
				$errList = array();
				$user = $this->_userBus->getUser($userid);
					
				//the user save changes
				if (!empty($_POST['txtEmail']) || !empty($_POST['txtLastName']) || !empty($_POST['txtFirstName']) || !empty($_POST['selGender']) || !empty($_POST['txtDOB']) || !empty($_POST['selOccupation'])
				 || !empty($_POST['txtPhone']) || !empty($_POST['txtCellPhone']) || ( !empty($_POST['txtNumber'])
				 && !empty($_POST['txtStreet']) && !empty($_POST['txtWard']) && !empty($_POST['txtDistrict']) && !empty($_POST['selCity']) ))
				{
					if (!empty($_POST['txtEmail'])) {
						$user->setEmail($_POST['txtEmail']);
					}
						
					if (!empty($_POST['txtLastName'])) {
						$user->setLastName($_POST['txtLastName']);
					}
						
					if (!empty($_POST['txtFirstName'])) {
						$user->setFirstName($_POST['txtFirstName']);
					}
						
					$user->setGender($_POST['selGender']);
					
					if (!empty($_POST['txtDOB'])) {
						$user->setDateOfBirth($_POST['txtDOB']);
					}
						
					$user->setOccupationID($_POST['selOccupation']);
					
					if (!empty($_POST['txtPhone'])) {
						$user->setTelephone($_POST['txtPhone']);
					}
					
					if (!empty($_POST['txtCellPhone'])) {
						$user->setCellPhone($_POST['txtCellPhone']);
					}
						
					$this->_userBus->updateUser($user);
						
					if (!empty($_POST['txtNumber']) && !empty($_POST['txtStreet']) && !empty($_POST['txtWard']) 
					&& !empty($_POST['txtDistrict']) && !empty($_POST['selCity'])) {
						$addressBus = new AddressBus();
						$address = $addressBus->getAddress($user->getAddressID());
						
						$address->setNumber($_POST['txtNumber']);
						$address->setStreet($_POST['txtStreet']);
						$address->setWard($_POST['txtWard']);
						$address->setDistrict($_POST['txtDistrict']);
						$address->setCity($_POST['selCity']);
						
						$addressBus->updateAddress($address);
					}					
					$notice = 'Cập nhật thành công thông tin người dùng';
				}
				
				$occupationList = $this->_busOccupation->getOccupationList();
				
				require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewUpdateUser.php';
			}
			else {
				$notice = 'PageNotFound';
				require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewNotice.php';
			}
		} else if ($getVars['type'] == 'blast') {
			if ($userID) {
				$user = $this->_userBus->getUser($userID);				
				if ($user) {
					$user->setBlast($_POST['txtBlast']);
					echo $this->_userBus->updateBlast($_POST['blast'], $userID);
				}
			}
		} else if ($getVars['type'] == 'address') {
			$locationBus = new LocationBus();
			
			$address = new AddressModel();
			$address->setAddressID($_POST['id']);
			$address->setCity($locationBus->getLocationFromID($_POST['city']));
			$address->setDistrict($locationBus->getLocationFromID($_POST['district']));
			$address->setLatitude(0);
			$address->setLongitude(0);
			$address->setNumber($_POST['number']);
			$address->setStreet($_POST['street']);			
			$address->setWard($locationBus->getLocationFromID($_POST['ward']));
			
			echo $this->_addressBus->updateAddress($address);
		}
	}
}
?>